<!DOCTYPE html>
<html> 
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>yukari world</title>
    <#include "/lib/base_source.ftl">
      <style type="text/css">
      </style>
  </head> 
  <body>
    <div class='whole-container'>
      <#include "/lib/header.ftl">
        <div>
          <ul class="breadcrumb">
            <li>抽奖管理</li><span class="divider">/</span>
            <li>弹幕抽奖</li>
          </ul>
        </div>
        <div class="container-fluid">
          <div class="row-fluid">
            <div class="container span12">
              <#include "/lib/alert.ftl">
                 <div class='main-content'>
                    <div class="well form-horizontal">
                        <div class='main-action'>
                    		<div class="control-group">
                                <label class="control-label"><span style="color:red">注意:</span></label>
                                <div class="controls">
                                    <span style="color:red">该抽奖请在礼物系统正常时使用，弹幕有小概率丢失，不放心的可以多发几遍弹幕</span>
                                </div>
                            </div>
                        	<form id="findForm">
                        		<div class="control-group">
	                                <label class="control-label">弹幕口令</label>
	                                <div class="controls">
	                                    <input type="text" id="chatmsg" name="chatmsg" value="${status.chatmsg!}" style="width:100px" <#if (status.drawChatmsgFlag || status.endTime??)  >readonly="readonly"</#if> />
	                                </div>
	                            </div>
                        		<div class="control-group">
	                                <label class="control-label">奖品数量</label>
	                                <div class="controls">
	                                    <input type="text" id="count" name="count" value="${status.count!}" style="width:100px" <#if (status.drawChatmsgFlag || status.endTime??)  >readonly="readonly"</#if> />
	                                </div>
	                            </div>
                        		<div class="control-group">
	                                <label class="control-label">粉丝牌子最小等级</label>
	                                <div class="controls">
	                                    <input type="text" id="minBl" name="minBl" value="${status.minBl!}" style="width:100px" <#if (status.drawChatmsgFlag || status.endTime??)  >readonly="readonly"</#if> />
	                                    <span style="color:red;">不限牌子请设为0，做限制时请佩戴196牌子，佩戴其他房间牌子无效</span>
	                                </div>
	                            </div>
                        		<#if status.drawChatmsgFlag>
                        		<div class="control-group">
	                                <label class="control-label">开始时间</label>
	                                <div class="controls">
	                                    ${status.startTime}
	                                </div>
	                            </div>
                        		<div class="control-group" id="lastDiv">
	                                <label class="control-label"></label>
	                                <div class="controls">
	                                    <a id="submitBtn" onclick="stop()" class="btn btn-info btn-sm">停止</a>
	                                </div>
	                            </div>
	                            <#elseif status.endTime??>
	                            <div class="control-group">
	                                <label class="control-label">开始时间</label>
	                                <div class="controls">
	                                    ${status.startTime}
	                                </div>
	                            </div>
	                            <div class="control-group">
	                                <label class="control-label">结束时间</label>
	                                <div class="controls">
	                                    ${status.endTime}
	                                </div>
	                            </div>
	                            <!--
	                            <div class="control-group">
	                                <label class="control-label">参与人数</label>
	                                <div class="controls">
	                                    ${status.size?c}
	                                </div>
	                            </div>
	                            -->
	                            <div class="control-group">
	                                <label class="control-label">中奖人</label>
	                                <div class="controls">
	                                    <#list status.authArray as auth>
	                                    	<div><span>Lv.${auth.lv}&nbsp;${auth.name}&nbsp;<#if (auth.fansLv > 0)><img src="${rc.contextPath}/images/chuizi2.png" />Lv.${auth.fansLv}</#if></span></div>
	                                    </#list>
	                                </div>
	                            </div>
	                            <div class="control-group" id="lastDiv">
	                                <label class="control-label"></label>
	                                <div class="controls">
	                                    <a id="submitBtn" onclick="clearStatus()" class="btn btn-info btn-sm">清除</a>
	                                </div>
	                            </div>
	                            <#else>
	                            <div class="control-group" id="lastDiv">
	                                <label class="control-label"></label>
	                                <div class="controls">
	                                    <a id="submitBtn" onclick="start()" class="btn btn-info btn-sm">开始</a>
	                                </div>
	                            </div>
                        		</#if>
                        	</form>
                        </div>
                    </div>
                    
                    <div class="table-content">
                    <#-- table -->
                        <table class="table-striped table-condensed table-hover" id="itemTable" style="width: 250px;max-width: 250px;margin-bottom: 20px;font-size:12px;word-break:break-all;word-wrap:break-word;">
                        </table>
                        <div class="pagination" id="itemPage"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
  <script>
    $(document).ready(function() {
	});
	
	var authMap = {};
	var authFlag = true;
	function getDrawAuth(){
		if (authFlag){
			authFlag = false;
			$.ajax({
				url : "${rc.contextPath}/admin/draw/chatmsg/authDetail",
		    	dataType : "json",
		    	type:'post',
		    	success:function(json){
		    		if (json.code == 0){
		    			var data = json.data;
		    			var table = $("#itemTable");
		    			for (var i=0;i<data.length;i++){
		    				var auth = data[i];
		    				if (authMap[auth.id]){
		    				} else {
		    					authMap[auth.id] = auth;
		    					var tr = $("<tr></tr>");
		    					
		    					var td1 = $("<td style='width:80%;'>Lv."+auth.lv+"&nbsp;"+auth.name+"&nbsp;<img src='${rc.contextPath}/images/chuizi2.png' />Lv."+auth.fansLv+"</td>");
		    					var td2 = $("<td style='width:20%;'>Lv."+auth.lv+"</td>");
		    					tr.append(td1).append(td2);
		    					table.prepend(tr);
		    				}
		    			}
		    		} else {
		    			alert("获取名单失败: "+json.message);
		    		}
		    		authFlag = true;
		    	}
			});
		}
	}
	
	var startFlag = true;
	function start(){
		if (startFlag){
			startFlag = false;
			var chatmsg = $("#chatmsg");
			var count = $("#count");
			var minBl = $("#minBl");
			chatmsg.prop("readonly",true);
			count.prop("readonly",true);
			minBl.prop("readonly",true);
			$.ajax({
				url : "${rc.contextPath}/admin/draw/chatmsg/start",
		    	dataType : "json",
		    	data :  {"chatmsg":chatmsg.val(),"count":count.val(),"minBl":minBl.val()},
		    	type:'post',
		    	success:function(json){
		    		if (json.code == 0){
		    			var lastDiv = $("#lastDiv");
		    			var div = $('<div class="control-group"><label class="control-label">开始时间</label><div class="controls">'+json.data+'</div></div>')
		    			lastDiv.before(div);
		    			var btn = $("#submitBtn");
		    			btn.attr("onclick","stop()");
		    			btn.html("停止");
		    		} else {
		    			alert("启动失败: "+json.message);
		    		}
		    		startFlag = true;
		    	}
			});
		}
	}
	
	var stopFlag = true;
	function stop(){
		if(stopFlag){
			stopFlag = false;
			$.ajax({
				url : "${rc.contextPath}/admin/draw/chatmsg/stop",
		    	dataType : "json",
		    	type:'post',
		    	success:function(json){
		    		if (json.code == 0){
		    			var lastDiv = $("#lastDiv");
		    			var div1 = $('<div class="control-group"><label class="control-label">结束时间</label><div class="controls">'+json.data.endTime+'</div></div>');
		    			var div2 = $('<div class="control-group"><label class="control-label">参与人数</label><div class="controls">'+json.data.size+'</div></div>');
		    			var nameDiv = $('<div class="controls"></div>');
		    			for (var i=0;i<json.data.authArray.length;i++){
		    				var auth = json.data.authArray[i];
		    				var txt = "Lv."+auth.lv+"&nbsp;"+auth.name+"&nbsp;";
	    					if (auth.fansLv > 0){
	    						txt = txt + "<img src='${rc.contextPath}/images/chuizi2.png' />Lv."+auth.fansLv
	    					}
		    				nameDiv.append("<div><span>"+txt+"</span></div>");
		    			}
		    			var div3 = $('<div class="control-group"><label class="control-label">中奖人</label></div>')
		    			div3.append(nameDiv);
		    			lastDiv.before(div1);
		    			//lastDiv.before(div2);
		    			lastDiv.before(div3);
		    			var btn = $("#submitBtn");
		    			btn.attr("onclick","clearStatus()");
		    			btn.html("清除");
		    		} else {
		    			alert("启动失败: "+json.message);
		    		}
		    		stopFlag = true;
		    	}
			});
		}
	}
	
	function clearStatus(){
		$.ajax({
			url : "${rc.contextPath}/admin/draw/chatmsg/clear",
	    	dataType : "json",
	    	type:'post',
	    	success:function(json){
	    		location.reload();
	    	}
		});
	}
	
	//var timeid = window.setInterval("getDrawAuth()",200);
	
	
		
   </script>
</body>
</html>